//
//  KSFeedBackFirstCollectionViewCell.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/20.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit
import SnapKit

struct questionBtnState {
    var btnPoint: NSInteger?
    var btnSelect: Bool?
}

protocol ChooseQuestionTypeDelegate: NSObjectProtocol {
    func chooseQuestionTypeCallBack(btnTag: [NSInteger],questionDescibe: String)
}

class KSFeedBackFirstCollectionViewCell: UICollectionViewCell {
    private var headLabel = UILabel()
    private var questionDescibeTxtView: UITextView!
    private var questionType: [questionBtnState] = []
    private let placeLabe = UILabel()
    private let numberLabel = UILabel()
    private var selectType: [NSInteger] = []
    weak var delegate: ChooseQuestionTypeDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        headLabel.text = "   请选择问题的类型"
        headLabel.font = UIFont.systemFont(ofSize: 15)
        headLabel.textColor = KSColor.kColor_3
        self.contentView.addSubview(headLabel)
        headLabel.snp.makeConstraints { (make) in
            make.top.right.left.equalTo(0)
            make.height.equalTo(40)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = KSColor.kColorViewBackground
        self.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(headLabel.snp.bottom).offset(0)
            make.right.left.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        let questionTypeArray = ["账号不存在","账号/密码错误","会员信息错误","银果数量不对","查看订单详情","其他问题"]
        let btnBackView = UIView()
        self.contentView.addSubview(btnBackView)
        btnBackView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(10)
            make.left.right.equalTo(0)
            make.height.equalTo(150)
        }
        
        for i in 0...questionTypeArray.count - 1 {
            let crosswiseSpace: CGFloat = 12
            let endWaysSpace: CGFloat = 12
            let btnW: CGFloat = (KSConstants.kScreenWidth - crosswiseSpace * 3)/2
            let btnH: CGFloat = 35
            let btnX: CGFloat = (btnW + crosswiseSpace) * CGFloat(i%2) + crosswiseSpace
            let btnY: CGFloat = (btnH + endWaysSpace) * CGFloat(i/2) + endWaysSpace
            
            let questionBtn = UIButton.init(frame: CGRect.init(x: btnX, y: btnY, width: btnW, height: btnH))
            questionBtn.layer.cornerRadius = 3
            questionBtn.layer.borderColor = KSColor.kColor_9.cgColor
            questionBtn.layer.borderWidth = 0.2
            questionBtn.tag = 10000 + i
            questionBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            questionBtn.setTitle(questionTypeArray[i], for: UIControlState.normal)
            questionBtn.setTitleColor(KSColor.kColor_9, for: .normal)
            questionBtn.setTitleColor(KSColor.kColorMainRed, for: .selected)
            questionBtn.setBackgroundImage(UIImage(named: "Account_FeedBack_Question"), for: .selected)
            questionBtn.setBackgroundImage(UIImage.init(named: ""), for: .normal)
            questionBtn.addTarget(self, action: #selector(chooseQuestType(button:)), for: .touchDown)
            btnBackView.addSubview(questionBtn)
            questionType.append(questionBtnState.init(btnPoint: i, btnSelect: false))
        }
        
        self.questionDescibeTxtView = UITextView()
        self.questionDescibeTxtView.delegate = self
        self.questionDescibeTxtView.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(self.questionDescibeTxtView)
        questionDescibeTxtView.snp.makeConstraints { (make) in
            make.top.equalTo(btnBackView.snp.bottom).offset(5)
            make.left.right.equalTo(10)
            make.height.equalTo(104)
        }
        
        placeLabe.text = "请简单扼要写明问题的原因或提供相关信息证明，有助于我们更快的帮您解决问题"
        placeLabe.font = UIFont.systemFont(ofSize: 14)
        placeLabe.textColor = KSColor.kColor_line
        placeLabe.numberOfLines = 0
        questionDescibeTxtView.addSubview(placeLabe)
        placeLabe.snp.makeConstraints { (make) in
            make.top.equalTo(questionDescibeTxtView).offset(5)
            make.left.equalTo(questionDescibeTxtView).offset(8)
            make.width.equalTo(KSConstants.kScreenWidth - 16)
        }
 
        numberLabel.text = "140"
        numberLabel.textColor = KSColor.kColor_9
        self.contentView.addSubview(numberLabel)
        numberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(questionDescibeTxtView.snp.bottom).offset(0)
            make.right.equalToSuperview().offset(0)
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.questionDescibeTxtView.resignFirstResponder()
    }
    
    @objc func chooseQuestType(button: UIButton) {
        button.isSelected = !button.isSelected
        
        let q = questionBtnState.init(btnPoint: button.tag, btnSelect: button.isSelected)
        questionType.remove(at: button.tag - 10000)
        questionType.insert(q, at: button.tag - 10000)
        
        if questionType.count > 0 {
            for i in 0...questionType.count - 1 {
                let obje: questionBtnState = questionType[i]
                if obje.btnSelect! {
                    self.selectType.append(obje.btnPoint!)
                }
            }
            self.delegate?.chooseQuestionTypeCallBack(btnTag: selectType, questionDescibe: questionDescibeTxtView.text)
        }
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KSFeedBackFirstCollectionViewCell: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.placeLabe.isHidden = true
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.placeLabe.isHidden = textView.text.isEmpty ? false : true
        self.delegate?.chooseQuestionTypeCallBack(btnTag: selectType, questionDescibe: questionDescibeTxtView.text)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        if !(textView.text.isEmpty) {
            numberLabel.text = "\(textView.text.characters.count)/140"
        }
    }
}
